﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using PeekService;

namespace PeekWebApp.Management.AjaxHandler
{
    /// <summary>
    /// UserMessageEditHandler 的摘要说明
    /// </summary>
    public class UserMessageEditHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            string name = "";
            string email = "";
            string message = "";
            string remark = "";
            string code = "";
            PeekService.UserMessage model = new PeekService.UserMessage();
            switch (action)
            {
                case "Answner": //答复
                    name = context.Request["name"].Trim();
                    remark = context.Request["remark"].Trim();
                    email = context.Request["email"];
                    message = context.Request["message"];
                    code = context.Request["hidCode"];
                    model = new PeekService.UserMessageService().GetModel(code);
                    if (model == null)
                    {
                        model = new PeekService.UserMessage();
                        model.Code = Guid.NewGuid().ToString();
                        model.Sort = new PeekService.ExtendService().GetSort("UserMessage");
                        model.CreateID = "";
                        model.CreateTime = DateTime.Now;
                    }
                    model.Email = email;
                    model.Name = name;
                    model.Remark = remark;
                    model.Message = message;
                    model.State = 2;//回答
                    new PeekService.UserMessageService().Update(model);
                     string str="您的留言内容如下：<br/><b>姓名：</b>"+model.Name+"<br/><b>邮箱：</b>"+email+"<br/><b>留言内容：</b><br/>"+model.Message+"<br/><b>答复：</b><br/><font color='red'>"+model.Remark+"</font>";
                        CommonService.SendEmail(email, str, "尊敬的"+model.Name+",您好",SendType.Message);
                    context.Response.Redirect("../UserMessage/List.aspx");
                    break;
                case "Refuse"://拒绝

                    name = context.Request["name"].Trim();
                    remark = context.Request["remark"].Trim();
                    email = context.Request["email"];
                    message = context.Request["message"];
                    code = context.Request["hidCode"];
                    model = new PeekService.UserMessageService().GetModel(code);
                    model.Email = email;
                    model.Name = name;
                    model.Remark = remark;
                    model.Message = message;
                    model.State = 3;//拒绝
                    new PeekService.UserMessageService().Update(model);
                    context.Response.Redirect("../UserMessage/List.aspx");
                    break;
                case "Delete":
                    try
                    {
                        code = context.Request["code"];
                        model = new PeekService.UserMessageService().GetModel(code);
                        if (model != null)
                        {
                            model.State = 1;
                            bool isTrue = new PeekService.UserMessageService().Update(model);
                            if (isTrue)
                            {                                
                                context.Response.Write(JsonConvert.SerializeObject("List.aspx"));
                            }
                            else
                            {
                                context.Response.Write(JsonConvert.SerializeObject("0"));
                            }
                        }

                    }
                    catch
                    {
                        context.Response.Write(JsonConvert.SerializeObject("0"));
                    }
                    break;
                case "Get":
                    string id = context.Request["hidCode"];
                    model = new PeekService.UserMessageService().GetModel(id);
                    context.Response.Write(PeekService.CommonService.ConvertToJSON(model));
                    break;
            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}